﻿namespace EasyWeb.Validation
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Globalization;

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class EWCultureNameValidationAttribute : ValidationAttribute
    {
        public override string FormatErrorMessage(string name)
        {
            return string.Format(CultureInfo.CurrentCulture, EWValidationResources.Validation_DefaultLanguage, name);
        }

        public override bool IsValid(object value)
        {
            var text = value as string;
            return !string.IsNullOrEmpty(text) && CultureInfo.GetCultureInfo(text) != null;
        }
    }
}
